/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
/*
 * 蛋白石组件
 */
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";

/**
 * * 蝰蛇 - 维系者
 * 
 * @param {server.Entity} object - 自身
 * 
 * @param {server.Entity} target - 目标
 */
export function Support(object: server.Entity, target: server.Entity) {
    /**
     * * 攻击次数
     */
    const count = object.getDynamicProperty('viper_support:count') as number ?? 0;
    // 当攻击次数 大于 32 次时
    if (count >= 32) {
        /**
         * * 生命值组件
         */
        const component = object.getComponent('minecraft:health');
        /**
         * * 当前生命值
         */
        const health = component?.currentValue ?? 0;
        // 扣除 50% 当前生命值
        component?.setCurrentValue(Math.floor(health * 0.5) + 1);
        // 播放 粒子 与 音效
        if (!object.dimension.getBlock(object.location)?.isValid()) return;
        native_tools.TrySpawnParticle(object.dimension, 'minecraft:knockback_roar_particle', object.getHeadLocation());
        native_tools.TrySpawnParticle(object.dimension, 'minecraft:knockback_roar_particle', object.getHeadLocation());
        native_tools.TrySpawnParticle(object.dimension, 'minecraft:knockback_roar_particle', object.getHeadLocation());
        object.dimension.playSound('mob.horse.skeleton.hit', object.location);
        return;
    };
    // 记录 攻击次数
    object.setDynamicProperty('viper_support:count', count + 1);
    /**
     * * 爆炸事件
     * 
     * @param args - 附加参数
     */
    const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
        // 验证 实体状态 是否正确
        if (!object || !target || !object.isValid() || !target.isValid()) return;
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            excludeTypes: ["minecraft:item", "minecraft:xp_orb", object.typeId],
            excludeFamilies: ['abyss'],
            location: args.location,
            maxDistance: 3,
            closest: 4
        };
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options);
        // 创建 元素伤害
        entitys.forEach(
            entity => {
                /**
                 * * 是否暴击
                 */
                const erupt = derived_tools.IsErupt(object);
                /**
                 * * 击退方向
                 */
                const direction = math_tools.Vector.difference(object.location, entity.location);
                /**
                 * * 本体与目标的距离
                 */
                const distance = math_tools.Vector.distance(object.location, entity.location);
                /**
                 * * 击退系数
                 */
                const mapping = (1 + distance / distance) * 3;
                // 执行击退
                entity.applyKnockback(direction.x, direction.z, mapping, direction.y * 2);
                // 创建 元素攻击
                achieve_plan.RunePowerAttack.BriefCreate(object, entity, erupt);
            }
        );
        native_tools.TrySpawnParticle(args.dimension, 'constant:excite_rune_orange', args.location);
    };
    // 创建 路径
    achieve_plan.PathExecute.Create('蝰蛇维系者-炮击轨迹', 1,
        {
            location_group: [object.getHeadLocation(), target.getHeadLocation()],
            particles: ['constant:track_rune_orange'],
            dimension: object.dimension,
            on_done: powerExplode,
            cooldown: 1,
            speed: 1
        }
    );
};

/**
 * * 森蚺 - 哨兵炮
 * 
 * @param {server.Entity} object - 自身
 * 
 * @param {server.Entity} target - 目标
 */
export function PythonSentinel(object: server.Entity, target: server.Entity) {
    /**
     * * 攻击次数
     */
    const count = object?.getDynamicProperty('python_sentinel:count') as number ?? 0;
    /**
     * * 获取 玩家 ID
     */
    const userId = object.getDynamicProperty('entity:contract_user') as string; if (!userId) return;
    // 当攻击次数 大于 64 次时
    if (count >= 64) {
        /**
         * * 哨兵炮 召唤物
         */
        const item = new server.ItemStack('item_armament:call_python_sentinel');
        /**
         * * 获取 玩家
         */
        const player = server.world.getEntity(userId);
        // 重置攻击计数
        object.setDynamicProperty('python_sentinel:count', 0);
        // 删除 渊鲸潜艇
        if (player) derived_tools.UnloadInventoryAndPackage(object, player as server.Player, item, '§l§m武器回收§r - ', []);
        return;
    };
    // 记录 攻击次数
    object.setDynamicProperty('python_sentinel:count', count + 1);
    /**
     * * 爆炸事件
     * 
     * @param args - 附加参数
     */
    const powerExplode = (args: data_format.ROUTE_ANNEX_ARGS) => {
        /**
         * * 过滤器参数
         */
        const options: server.EntityQueryOptions = {
            families: ['monster'],
            location: args.location,
            maxDistance: 4,
            closest: 4
        };
        /**
         * * 契约者标识符
         */
        const contract = object.getDynamicProperty('entity:contract_user') as string;
        /**
         * * 获取实体列表
         */
        const entitys = args.dimension.getEntities(options);
        // 过滤 契约者 与 锁定的目标
        entitys.filter(entity => entity.id !== contract && entity.id !== target?.id);
        // 创建 元素伤害
        entitys.forEach(
            entity => {
                /**
                 * * 是否暴击
                 */
                const erupt = derived_tools.IsErupt(object);
                /**
                 * * 击退方向
                 */
                const direction = math_tools.Vector.difference(object.location, entity.location);
                /**
                 * * 本体与目标的距离
                 */
                const distance = math_tools.Vector.distance(object.location, entity.location);
                /**
                 * * 击退系数
                 */
                const mapping = (1 + distance / distance) * 3;
                // 执行击退
                entity.applyKnockback(direction.x, direction.z, mapping, direction.y * 2);
                // 创建 元素伤害
                achieve_plan.RunePowerAttack.BriefCreate(object, entity, erupt);
            }
        );
        // 对目标单独结算一次伤害
        achieve_plan.RunePowerAttack.BriefCreate(object, target, derived_tools.IsErupt(object));
        // 创建 粒子效果
        native_tools.TrySpawnParticle(args.dimension, 'constant:excite_rune_orange', args.location);
    };
    // 创建 路径
    achieve_plan.PathExecute.Create('森蚺哨兵炮-炮击轨迹', 1,
        {
            location_group: [object.getHeadLocation(), target.getHeadLocation()],
            particles: ['constant:track_rune_orange'],
            dimension: object.dimension,
            on_done: powerExplode,
            cooldown: 1,
            speed: 1
        }
    );
};